package com.github.cyf.word.util;

import com.aspose.words.Document;
import com.aspose.words.HtmlSaveOptions;
import com.aspose.words.SaveFormat;
import com.github.cyf.license.LicenseUtil;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @author chenyifan
 * @create 2024-11-29 15:56
 */
public class AsposeWordUtil {

    public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

    /**
     * word 转 pdf
     * @param wordPath
     * @param pdfPath
     * @throws Exception
     */
    public static void wordToPdf(String wordPath, String pdfPath) throws Exception {
        Document doc = new Document(wordPath);
        File pdf = new File(pdfPath);
        if (!pdf.getParentFile().exists()) {
            pdf.getParentFile().mkdirs();
        }
        try (FileOutputStream fos = new FileOutputStream(pdf)) {
            doc.save(fos, SaveFormat.PDF);
        }
    }

    /**
     * word 转 html
     * @param wordPath word 文档路径
     * @return
     */
    public static String wordToHtml(String wordPath) {
        try(FileInputStream is = new FileInputStream(wordPath)) {
            return wordToHtml(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * word 转 html
     * @param is  输入流
     * @return
     */
    public static String wordToHtml(InputStream is) {
        LicenseUtil.getWordsLicense();
        String html;
        try {
            Document doc = new Document(is);
            // 设置输出格式为 HTML
            HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);
            options.setExportXhtmlTransitional(true);
            // 设置将图像作为 Base64 编码嵌入 HTML 中
            options.setExportImagesAsBase64(true);
            options.setExportPageSetup(true);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            doc.save(os, options);
            html = os.toString(DEFAULT_CHARSET.name());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return html;
    }


    /**
     * html 转 word
     * @param html
     * @param os
     * @return
     */
    public static void htmlToWord(String html, OutputStream os) {
        LicenseUtil.getWordsLicense();
        ByteArrayInputStream is = new ByteArrayInputStream(html.getBytes(DEFAULT_CHARSET));
        try {
            Document doc = new Document(is);
            doc.save(os, SaveFormat.DOCX);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * html 转 word
     * @param html
     * @param exportPath 导出路径
     */
    public static void htmlToWord(String html, String exportPath) {
        File file = new File(exportPath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try (FileOutputStream os = new FileOutputStream(file)) {
            htmlToWord(html, os);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 刷新目录
     * @param filePath
     */
    public static void refreshDirectory(String filePath) {
        LicenseUtil.getWordsLicense();
        Document doc = null;
        try {
            doc = new Document(filePath);
            doc.updateFields();
            doc.save(filePath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}